package com.xiaoerkeji.eshop.utils;

import android.os.Message;
import android.text.TextUtils;

import com.lt.bean.RequestResult;
import com.xiaoerkeji.eshop.bean.RequestInfo;
import com.xiaoerkeji.eshop.xeConstant;

public class ComUtils
{
    public static RequestInfo formatRequest(Message message)
    {
        RequestResult result = (RequestResult) message.obj;
        RequestInfo info = new RequestInfo();
        if (result == null)
        {
            info.status = 901;
            info.msg = "网络请求错误";
        } else
        {
            if (result.status == 200)
            {
                info = JsonHelper.parseObject(result.data, RequestInfo.class);
                if (info == null)
                {
                    info = new RequestInfo();
                    info.status = result.status;
                    info.msg = "数据解析失败";
                }
            } else
            {
                info.msg = String.format("访问服务器出现错误Code:%s", result.status);
            }
            if (result.status != 200)
            {
                info.status = result.status;
            }
            info.what = result.what;
            info.mark = result.mark;
        }
        return info;
    }

    public static boolean isApk(String url)
    {
        if (TextUtils.isEmpty(url))
        {
            return false;
        }
        if (url.endsWith(xeConstant.apk))
        {
            return true;
        }
        if (url.contains(xeConstant.apk) && url.contains("?"))
        {
            String tmp = url.substring(0, url.indexOf("?"));
            return tmp.endsWith(xeConstant.apk);
        }
        return false;
    }

    public static boolean isUrl(String url)
    {
        if (!TextUtils.isEmpty(url))
        {
            return url.startsWith("https://") || url.startsWith("http://") || url.startsWith("www.");
        }
        return false;
    }
}
